package com.starlight.flower_server.infrastructure;

import lombok.Data;

import java.time.LocalDateTime;

/**
 * @Author yfy2001
 * @date 2023/4/4 13:22
 */
@Data
public class FlowerServerResponse<T> {
    /**
     * 时间
     */
    private LocalDateTime time;
    /**
     * 状态
     */
    private Status status;
    /**
     * 异常码
     */
    private Integer code;
    /**
     * 信息
     */
    private String message;
    /**
     * 数据
     */
    private T data;

    /**
     * 成功返回
     * @param data
     * @return
     * @param <T>
     */
    public static <T> FlowerServerResponse<T> success(T data){
        FlowerServerResponse<T> flowerServerResponse =new FlowerServerResponse<>();
        flowerServerResponse.setTime(LocalDateTime.now());
        flowerServerResponse.setCode(Status.OK.getCode());
        flowerServerResponse.setStatus(Status.OK);
        flowerServerResponse.setMessage("操作成功！");
        flowerServerResponse.setData(data);
        return flowerServerResponse;
    }

    /**
     * 失败返回
     * @param data
     * @return
     * @param <T>
     */
    public static <T> FlowerServerResponse<T> fail(Status status, T data){
        FlowerServerResponse<T> flowerServerResponse =new FlowerServerResponse<>();
        flowerServerResponse.setTime(LocalDateTime.now());
        flowerServerResponse.setCode(status.getCode());
        flowerServerResponse.setStatus(status);
        flowerServerResponse.setMessage("操作失败!"+status.getMessage());
        flowerServerResponse.setData(data);
        return flowerServerResponse;
    }
}
